package com.siysoft.tc.remind.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.siysoft.tc.base.bean.to.Remind;
import com.siysoft.tc.base.bean.to.User;
import com.siysoft.tc.remind.service.IRemindService;
import com.siysoft.util.COMMON;
import com.tc.base.dao.BaseDAO;

@Service
public class RemindServiceImpl implements IRemindService{
	@Autowired
	BaseDAO baseDao;
	
	// type 0 :class_room
	// type 1 :system_message
	// type 2: personal_message

	public Remind getRemindByUserId(Integer user_id){
		String hql = "from Remind where user_id = " + user_id ;
		List<Remind> remindList = (List<Remind>)baseDao.findByHQL(hql);
		if(COMMON.isEmpty(remindList))return null;
		else return remindList.get(0);
	}
	
	@Override
	public Boolean hasNewRemind(Integer user_id, Integer type) {
		// TODO Auto-generated method stub
		Remind remind = getRemindByUserId(user_id);
		int result = 0;
		if(COMMON.isEmpty(remind))return false;
		else {
			if(type == 0)result = remind.getClass_room_remind();
			else if(type == 1)result = remind.getSystem_message_remind();
			else if(type == 2)result = remind.getPersonal_message_remind();
			else return false;
		}
		return (result==0)?false:true;
	}

	@Override
	public Boolean addNewRemind(Integer type,Integer data_id,Integer user_id)  throws Exception {
		// TODO Auto-generated method stub
		Set<Integer> userIdList = getRelativeRemindUserList(type,data_id,user_id);
		if(COMMON.isEmpty(userIdList))return true;
		String sql0 = "select user_id from db_remind where user_id in ( " + SetToStr(userIdList) + " )";
		List<Integer> exist_in_db_remind_user_id_list = (List<Integer>) baseDao.findBySQL(sql0, null, -1, -1);
		List<Integer> not_exist_in_db_remind_user_id_list = ExcludeFromSet(exist_in_db_remind_user_id_list,userIdList);
		//对于表中已经存在于 user_id_list中的 id，执行update操作。
		if(!COMMON.isEmpty(exist_in_db_remind_user_id_list))
		{
			String sql = "";
			if(type == 0)
				sql = "update db_remind set class_room_remind = 1 where user_id in ("
					 + ListToStr(exist_in_db_remind_user_id_list) +")";
			else if(type == 1)
				sql = "update db_remind set system_message_remind = 1 where user_id in ("
						 + ListToStr(exist_in_db_remind_user_id_list) +")";
			else if(type == 2)
				sql = "update db_remind set personal_message_remind = 1 where user_id in ("
						 + ListToStr(exist_in_db_remind_user_id_list) +")";
			baseDao.executeSql(sql);
		}
		//对于表中已经并不存在于 user_id_list中的 id，执行insert操作。
		if(!COMMON.isEmpty(not_exist_in_db_remind_user_id_list))
		{
			String sql = "insert into db_remind (" +
					"user_id,class_room_remind,system_message_remind,personal_message_remind) " +
					"values " + 
					 FormatInsertSqlStr(not_exist_in_db_remind_user_id_list,type);
			System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa:" + sql);
			baseDao.executeSql(sql);
		}
		
		return true;
	}



	private String FormatInsertSqlStr(
			List<Integer> not_exist_in_db_remind_user_id_list, Integer type) {
		// TODO Auto-generated method stub
		if(type == 0)
		{
			String result = "";
			for (Integer integer : not_exist_in_db_remind_user_id_list) {
				result = result + "(";
				result = result + integer + ",1,0,0),";
			}
			return result.substring(0,result.length()-1);
		}
		if(type == 1)
		{
			String result = "";
			for (Integer integer : not_exist_in_db_remind_user_id_list) {
				result = result + "(";
				result = result + integer + ",0,1,0),";
			}
			return result.substring(0,result.length()-1);
		}
		if(type == 2)
		{
			String result = "";
			for (Integer integer : not_exist_in_db_remind_user_id_list) {
				result = result + "(";
				result = result + integer + ",1,0,1),";
			}
			return result.substring(0,result.length()-1);
		}
		return null;
	}

	private List<Integer> ExcludeFromSet(List<Integer> exist_in_db_update_user_id_list, Set<Integer> user_id_list) {
		// TODO Auto-generated method stub
		List<Integer> not_exist_in_db_update_user_id_list = new ArrayList<Integer>();
		for (Integer integer : exist_in_db_update_user_id_list) {
			user_id_list.remove(integer);
		}
		for (Integer integer : user_id_list) {
			not_exist_in_db_update_user_id_list.add(integer);
		}
		return not_exist_in_db_update_user_id_list;
	}

	private String SetToStr(Set<Integer> user_id_list) {
		// TODO Auto-generated method stub
		//转换成str 例如：1,2,3 格式的
		if(COMMON.isEmpty(user_id_list))return "";
		String str = user_id_list.toString();
		System.out.println("strstrstrstr" + str);
		return str.substring(1,str.length() - 1);
	}
	
	private String ListToStr(List<Integer> exist_in_db_remind_user_id_list) {
		// TODO Auto-generated method stub
		if(COMMON.isEmpty(exist_in_db_remind_user_id_list))return "";
		Set<Integer> set = new HashSet<Integer>();
		for (Integer integer : exist_in_db_remind_user_id_list) {
			set.add(integer);
		}
		return SetToStr(set);
	}

	/**
	 * 	type 0 : class_room:
		data_id 房间id user_id 触发人id
		type 1 : system_message:
		data_id和user_id都没用
		type 2: personal_message:
		data_id 被呼叫人 user_id没用。
	 * @throws Exception 
	 * **/
	private Set<Integer> getRelativeRemindUserList(Integer type,Integer data_id,Integer user_id ) throws Exception {
		// TODO Auto-generated method stub
		Set<Integer> userIdList = new HashSet<Integer>();
		String hql = "";
		//如果是房间提醒，则根据db_roomchart表中记录相应的reservation_id来获得相关的预约单所有用户.
		if(type == 0)
		{
			String sql = "select user_id from db_user_reservation where reservation_id = " + data_id;
			List<Map<String,Object>> list = (List<Map<String,Object>>)baseDao.findBySQLToMap(sql, null, -1, -1);
			for (Map<String, Object> map : list) {
				userIdList.add(Integer.valueOf(map.get("user_id").toString()));
			}
			//将触发人自己从列表中移除。
			userIdList.remove(user_id);
		}
		//如果是系统消息,则给平台所有用户发消息
		else if(type == 1)
		{
			hql = "from User";
			List<User> user_list = (List<User>)baseDao.findByHQL(hql);
			for (User user : user_list) {
				userIdList.add(user.getUser_id());
			}
		}
		//如果是私人消息,则发送给被呼叫人则ok。
		else if(type == 2)
		{
			userIdList.add(data_id);
		}
		return userIdList;
	}

	@Override
	public Boolean cancelRemind(Integer user_id, Integer type) {
		// TODO Auto-generated method stub
		Remind remind =  getRemindByUserId(user_id);
		if(COMMON.isEmpty(remind))return false;
		if(type == 0)remind.setClass_room_remind(0);
		else if (type == 1)remind.setSystem_message_remind(0);
		else if (type == 2)remind.setPersonal_message_remind(0);
		else return false;
		return true;
	}
	
	
}
